package com.emellend.api.s;
/**
 * spinner下拉列表的简单应用，注意是通过资源文件填充适配器
 */
import com.emellend.api.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class Spinner1 extends Activity {
	private Spinner mSpinner1;
	private Spinner mSpinner2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spinner_1);
		init();
	}

	private void init() {
		mSpinner1 = (Spinner) this.findViewById(R.id.spinner1);
		mSpinner2 = (Spinner) this.findViewById(R.id.spinner2);
		setAdapter(mSpinner1,R.array.colors);
		setAdapter(mSpinner2, R.array.planets);
		addListener();
	}

	private void setAdapter(Spinner spinner, int resId) {
		ArrayAdapter<CharSequence> adapter=
				ArrayAdapter.createFromResource(this, resId, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
	}
	
	private void addListener() {
		
		mSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				showToast("Spinner1: position=" + position + " id=" + id);
			}

			public void onNothingSelected(AdapterView<?> parent) {
				showToast("Spinner1: unselected");
			}
		});
		mSpinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
			
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				showToast("Spinner2: position=" + position + " id=" + id);
			}
			
			public void onNothingSelected(AdapterView<?> parent) {
				showToast("Spinner2: unselected");
			}
		});
	}
	
	private void showToast(CharSequence msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
